<?php

namespace app\model;

use think\Model;

class Video extends Model
{
	// 设置当前模型对应的数据表
	protected $name = 'video';
	// 设置当前模型主键
	protected $pk = 'id';

	public function getDataByIds(array $ids, string $fields = 'id', string $order = 'sort desc')
	{
		$data = $this::where(['id' => $ids])->field($fields)->order($order)->select();
		return $data ? $data->toArray() : [];
	}

	public function getRecommendedData(string $fields = 'id', int $page, int $limit, string $order = 'sort desc')
	{
		$offset = ($page - 1) * $limit;
		$total = $this::where([
								  'is_recommended' => 1,
								  'is_delete'      => 0,
								  'is_shelves'     => 1
							  ])->count();
		if ($total == 0) {
			return [
				'total_page' => 0,
				'total'      => 0,
				'data'       => []
			];
		}
		$totalPage = ceil($total / $limit);
		$data = $this::field($fields)->where([
												 'is_recommended' => 1,
												 'is_delete'      => 0,
												 'is_shelves'     => 1
											 ])->order($order)->limit($offset, $limit)->select();
		$data = $data ? $data->toArray() : [];
		return [
			'total_page' => $totalPage,
			'total'      => $total,
			'data'       => $data
		];
	}

	public function getDataByVideoTypeId($videoTypeId, $page = 1, $limit = 10, $fields = 'id', $order = 'sort desc')
	{
		$offset = ($page - 1) * $limit;
		$total = $this::where([
								  'video_type_id' => $videoTypeId,
								  'is_delete'     => 0,
								  'is_shelves'    => 1
							  ])->count();
		$totalPage = ceil($total / $limit);
		$data = $this::where([
								 'video_type_id' => $videoTypeId,
								 'is_delete'     => 0,
								 'is_shelves'    => 1
							 ])->field($fields)->order($order)->limit($offset, $limit)->select();
		$data = $data ? $data->toArray() : [];
		return [
			'total'      => $total,
			'total_page' => $totalPage,
			'data'       => $data
		];
	}

	public function getInfoById(int $id, $fields = '*')
	{
		$data = self::where(['id' => $id])->field($fields)->find();
		return $data ? $data->toArray() : [];
	}

	public function getPagingDataByWhere(array $where, int $page = 1, int $limit = 10, string $fields = '*', string $order = 'id desc')
	{
		$offset = ($page - 1) * $limit;
		if ($offset < 0) $offset = 0;
		$total = $this::where($where)->count();
		if ($total == 0) return [
			'total'      => 0,
			'total_page' => 0,
			'data'       => []
		];
		$totalPage = ceil($total / $limit);
		$data = $this::where($where)->field($fields)->order($order)->limit($offset, $limit)->select()->toArray();
		return [
			'total'      => $total,
			'total_page' => $totalPage,
			'data'       => $data
		];
	}

	public function getDataByWhere(array $where, string $fields = '', string $order = 'sort desc', int $limit = 0)
	{
		$model = $this::where($where)->field($fields)->order($order);
		if ($limit) {
			$model->limit($limit);
		}
		$data = $model->select();
		return $data ? $data->toArray() : [];
	}

	public function getDataByLikeName(string $name, string $fields = '')
	{
		$data = $this::whereLike('name', '%' . $name . '%')->where([
																	   'is_delete'  => 0,
																	   'is_shelves' => 1
																   ])->field($fields)->select();
		return $data ? $data->toArray() : [];
	}

	public function getGiveCount($where)
	{
		$data = $this::where($where)->field('give_count')->select();
		return $data ? $data->toArray() : [];
	}

	public function edit(array $params)
	{
		if (empty($params['id'])) {
			$data = $this::create($params);
			return $data->id;
		}
		$this::update($params);
		return $params['id'];
	}
}